package mysql

import (
	"bluebell_qimi/models"
	"strings"
	"time"
)

// 创建帖子
//1. sqlx版
/*
func CreatePost(p *models.Post) (err error) {
	sql := `insert into post(
		post_id,title,content,author_id,community_id)
		values(?,?,?,?,?)`
	_, err = db.Exec(sql, p.ID, p.Title, p.Content, p.AuthorID, p.CommunityID)
	return
}
*/

// 2. gorm版
func CreatePost(p *models.Post) (err error) {
	// 设置创建时间为当前时间
	p.CreateTime = time.Now()

	// 使用 GORM 的 Create 方法插入记录
	err = db.Create(p).Error
	return

	//resutlt := db.Select("ID", "Title", "Content", "AuthorID", "CommunityID").Create(&p)
	//err = resutlt.Error
	//return
}

// 根据id查询帖子
//1. sqlx版
/*
func GetPostById(pid int64) (post *models.Post, err error) {
	post = new(models.Post)
	sqlStr := `select post_id,title,content,author_id,community_id,create_time
				from post where post_id=?`

	err = db.Get(post, sqlStr, pid)
	return
}
*/

// 2. gorm版
func GetPostById(pid int64) (post *models.Post, err error) {
	post = new(models.Post)
	err = db.First(&post, "post_id=?", pid).Error
	return
}

// 获取所有帖子列表
//1. sqlx版
/*func GetPostList(page, size int64) (posts []*models.Post, err error) {
	//方法1
	//跳过前面2行，返回1行
	//select id,post_id,title,content,author_id,community_id,create_time from post limit 1 offset 2;
	//sqlStr := `select post_id,title,content,author_id,community_id,create_time
	//			from post limit ? offset ?`
	//posts = make([]*models.Post, 0, 2) //不要写成posts= make([]models.Post,2)，因为0代表长度，2代表用量，查询是limit2
	//err = db.Select(&posts, sqlStr, size, (page-1)*size)

	//方法2
	//跳过前面1行，返回2行数据
	//select id,post_id,title,content,author_id,community_id,create_time from post limit 1, 2;
	sqlStr := `select post_id,title,content,author_id,community_id,create_time
				from post order by create_time desc limit ?,?`
	posts = make([]*models.Post, 0, 2) //不要写成posts= make([]models.Post,2)，因为0代表长度，2代表用量，查询是limit2
	err = db.Select(&posts, sqlStr, (page-1)*size, size)
	return
}*/

// 2. gorm版
func GetPostList(page, size int64) (posts []*models.Post, err error) {
	//方法1
	//跳过前面2行，返回1行
	//select id,post_id,title,content,author_id,community_id,create_time from post limit 1 offset 2;
	//sqlStr := `select post_id,title,content,author_id,community_id,create_time
	//			from post limit ? offset ?`
	//posts = make([]*models.Post, 0, 2) //不要写成posts= make([]models.Post,2)，因为0代表长度，2代表用量，查询是limit2
	//err = db.Select(&posts, sqlStr, size, (page-1)*size)

	//方法2
	//跳过前面1行，返回2行数据
	//select id,post_id,title,content,author_id,community_id,create_time from post limit 1, 2;

	posts = make([]*models.Post, 0, 2) //不要写成posts= make([]models.Post,2)，因为0代表长度，2代表用量，查询是limit2
	err = db.Limit(int(size)).Offset(int((page - 1) * size)).Find(&posts).Error
	return
}

// 升级版获取帖子列表信息,照给定的ids顺序返回
//1. sqlx
/*func GetPostListByIDs(ids []string) (postList []*models.Post, err error) {
	sqlStr := `select post_id,title,content,author_id,community_id,create_time
			from post
			where post_id in (?)
			order by find_in_set(post_id,?)`

	query, args, err := sqlx.In(sqlStr, ids, strings.Join(ids, ","))
	if err != nil {
		return nil, err
	}
	query = db.Rebind(query)
	err = db.Select(&postList, query, args...)
	return
}*/

// 2. gorm
func GetPostListByIDs(ids []string) (postList []*models.Post, err error) {
	postList = make([]*models.Post, 0, len(ids))
	err = db.Debug().Raw("select post_id,title,content,author_id,community_id,create_time "+
		"from posts "+
		"where post_id in (?) "+
		"order by find_in_set(post_id,?)", ids, strings.Join(ids, ",")).Scan(&postList).Error
	return
}
